package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.R;
import com.hongxun.design.bean.PhotoBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.HashMap;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/4/27.
 */
public class PhotoGraphGridAdapter extends BaseAdapter {
    List<PhotoBean> itemBeanList;
    Context context;
    boolean isdelect = false;
    LayoutInflater inflater;
    int type;

    public PhotoGraphGridAdapter(List<PhotoBean> itemBeanList, Context context, int type) {
        this.itemBeanList = itemBeanList;
        this.context = context;
        this.type = type;
        inflater = LayoutInflater.from(context);
    }

    public void setDelect(boolean isdelect) {
        this.isdelect = isdelect;
    }

    public boolean getDelect() {
        return this.isdelect;
    }

    @Override
    public int getCount() {
        return itemBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        synchronized (inflater) {
            if (convertView == null)
                convertView = inflater.inflate(R.layout.gridview_item_photograph, null);
            PhotoBean itemBean = itemBeanList.get(i);
            ViewHolerHelper viewHolerHelper = new ViewHolerHelper(convertView);
            RelativeLayout item = viewHolerHelper.getView(R.id.photograph_grid_item);
            SimpleDraweeView icon = viewHolerHelper.getView(R.id.photograph_grid_icon);
            TextView textView = viewHolerHelper.getView(R.id.photograph_grid_name);
            //TextView delect = viewHolerHelper.getView(R.id.photograph_grid_delect);

            if ((itemBean.getUrl() != null && itemBean.getUrl().length() == 0)
                    || itemBean.getFlag().equals(MString.del)) {
                textView.setVisibility(View.VISIBLE);
                icon.setVisibility(View.GONE);
            } else {
                //加载服务器图片
                icon.setImageURI(UriUtil.getUrl(itemBean.getUrl(), type));
                // MyLog.L("HTTP_   itemBean.getUrl()=" + UriUtil.getUri(itemBean.getUrl()));
                //MyLog.L("HTTP_   itemBean.getPhoto_thumb()=" + UriUtil.getUri(itemBean.getPhoto_thumb()));
                icon.setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
            }
            item.setOnClickListener(itemBean.getListener());
//          delect.setVisibility(View.GONE);
//          if (isdelect) {
//            delect.setVisibility(View.VISIBLE);
//            delect.setOnClickListener(new MyClickListener(i) {
//                @Override
//                public void onMyClick(View view, int position) {
//                    itemBeanList.remove(position);
//                    notifyDataSetChanged();
//                }
//            });
//        }
            switch (type) {
                case Constant.TypePosition:

                    break;
                case Constant.TypePassageway:

                    break;
                case Constant.TypeEnvironmental:

                    break;
            }
            return convertView;
        }
    }

    public class ViewHolerHelper {
        private HashMap<Integer, View> mViews;
        View convertView;

        public ViewHolerHelper(View convertView) {
            mViews = new HashMap<Integer, View>();
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item_home_head, null);
            }
            convertView.setTag(this);
            this.convertView = convertView;
        }

        public <T> T getView(Integer R_Id) {
            View view = null;
            if (!mViews.containsKey(R_Id)) {
                view = convertView.findViewById(R_Id);
                if (view != null) {
                    mViews.put(R_Id, view);
                }
            } else {
                view = mViews.get(R_Id);
            }
            return (T) view;
        }
    }

    abstract class MyClickListener implements View.OnClickListener {
        int position;

        MyClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            onMyClick(view, position);
        }

        public abstract void onMyClick(View view, int position);
    }
}
